package com.woniuxy.rbac.web.api;

import com.woniuxy.rbac.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Auther: 老谭（<a href="http://www.woniuxy.com">蜗牛学苑</a>）
 */
@RestController
@RequestMapping("upload")
public class UploadController {
    @Value("${images.location}")
    private String uploadLocation;

    @PostMapping("avatar")
    public ResponseResult<String> uploadIamage(MultipartFile file){
        String newFileName="111";
        String suffix=".jpg";
        File f=new File(uploadLocation+"/"+newFileName+suffix);
        try {
            file.transferTo(f);
        }catch (IOException e){
            e.printStackTrace();
            return ResponseResult.FAIL;
        }
        return new ResponseResult(newFileName+suffix);
    }
}
